using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Client.DAL.Services;
using Silverlight.Weblog.Client.DAL.ViewModel;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Server.DAL;

namespace Silverlight.Weblog.Client.Default.Widgets
{
    public class CategorySiderbarWidgetViewModel : ViewModelBase
    {
        public CategorySiderbarWidgetViewModel(Category model)
        {
            _model = model;

            this.DisplayCategoryCommand = new DelegateCommand<Category>(OnDisplayCategoryCommand);

            this.RaiseNavigateToCategoryEvent = new DelegateCommand<Category>(c => 
                IoC.Get<IEventAggregator>().GetEvent<DisplayCategoryEvent>().Publish(c));
        }

        private void OnDisplayCategoryCommand(Category obj)
        {
            
        }

        #region Model
        private Category _model;
        public Category Model
        {
            get { return _model; }
            set
            {
                if (_model != value)
                {
                    _model = value;
                    RaisePropertyChanged("Model");
                    RaisePropertyChanged<CategorySiderbarWidgetViewModel, IEnumerable<CategorySiderbarWidgetViewModel>>((CategorySiderbarWidgetViewModel vm) => vm.ChildrenCategories);
                    RaisePropertyChanged((CategorySiderbarWidgetViewModel vm) => vm.RssUrl);
                }

            }
        }
        #endregion Model

        #region DisplayCategoryCommand
        private ICommand _displayCategoryCommand;
        public ICommand DisplayCategoryCommand
        {
            get { return _displayCategoryCommand; }
            set
            {
                if (_displayCategoryCommand != value)
                {
                    _displayCategoryCommand = value;
                    RaisePropertyChanged("DisplayCategoryCommand");
                }

            }
        }
        #endregion DisplayCategoryCommand

        public Uri RssUrl
        {
            get
            {
                if (Model == null)
                    return null;

                return new Uri(string.Format("{0}/Feed/rss/{1}/{2}", 
                                             Model.User.HomeUrl, 
                                             Model.User.Username,
                                             Model.Name), 
                               UriKind.RelativeOrAbsolute);
            }
        }

        public IEnumerable<CategorySiderbarWidgetViewModel> ChildrenCategories
        {
            get
            {
                if (Model != null)
                    foreach (Category curParentlessCategory in Model.ChildCategories.Where(c => c.ParentCategory == Model))
                        yield return new CategorySiderbarWidgetViewModel(curParentlessCategory);
            }
        }

        public ICommand RaiseNavigateToCategoryEvent { get; set; }

    }

}